package com.example.smartcitymovie.common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.widget.ViewPager2;

import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.ViewPagerTwoAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.List;

public class TVPFragment extends LinearLayout {
    View view;
    private TabLayout tablayout;
    private ViewPager2 viewPagerTwo;


    public TVPFragment(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.tvp_fragment_layout,this);
        tablayout = (TabLayout) view.findViewById(R.id.tablayout);
        viewPagerTwo = (ViewPager2) view.findViewById(R.id.viewPagerTwo);
    }

    public void setAdapter(FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> list, List<String> menus){
        viewPagerTwo.setAdapter(new ViewPagerTwoAdapter(fragmentManager,lifecycle,list));
        new TabLayoutMediator(tablayout, viewPagerTwo, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(menus.get(position));
            }
        }).attach();
    }
}
